package aufgabe2;

public class MatrixArray implements MatrixInterface<MatrixArray> {

	private double[][] matrix;

	MatrixArray(double[][] matrix) {
		this.matrix = matrix;
	}

	@Override
	public void add(MatrixArray m) {
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				matrix[i][j] = matrix[i][j] + m.getMatrixStructure()[i][j];
			}

		}
	}

	@Override
	public void skaMul(double f) {
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				matrix[i][j] = matrix[i][j] * f;
			}
		}

	}

	@Override
	public void mul(MatrixArray m) {
		double[][] temp = new double[matrix.length][matrix[0].length];
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				for (int k = 0; k < matrix[0].length; k++) {
					temp[i][j] = temp[i][j] + matrix[i][k]
							* m.getMatrixStructure()[k][j];
				}
			}

		}
		matrix = temp;
	}

	public double[][] getMatrixStructure() {
		return matrix;
	}

	public String toString() {
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				str.append(matrix[i][j] + " ");
			}
			str.append("\n");
		}
		return str.toString();
	}

	/**
	 * Ueberprueft ob zwei Matrizen gleich sind. Dabei wird der Datentyp nicht
	 * berücksichtigt. Es wird ueberprueft ob an Stelle ij beide Matrizen das
	 * selbe Element gespeichert haben.
	 * 
	 * @param m
	 *            Matrix
	 * @return True wenn die Matrizen gleich sind. False wenn nicht.
	 */

	public boolean equals(MatrixArray m) {
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				if (matrix[i][j] != m.getMatrixStructure()[i][j])
					return false;
			}
		}
		return true;
	}
	
	public int lenght(){
		int count = 0;
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				count++;
			}
		}
		return count;
		
	}
	

	@Override
	public void pow(int i) {
		MatrixArray erg = new MatrixArray(matrix);
		for (int j = 0; j < i - 1; j++) {
			erg.mul(this);
		}
		matrix = erg.getMatrixStructure();
	}

	@Override
	public void pot(int i) {
		MatrixArray erg = new MatrixArray(matrix);

		if (i == 0) {
			for (int j = 0; j < matrix.length; j++) {
				for (int k = 0; k < matrix.length; k++) {
					if (i == j) {
						matrix[i][j] = 1.0;
					} else {
						matrix[i][j] = 0.0;
					}
				}
			}
		}

		if (i == 1) {
			matrix = erg.getMatrixStructure();
		}

		else if (i == 2) {
			erg.mul(this);

		}

		else if (i % 2 == 1) {
			erg.pot(i - 1);
			erg.mul(this);

		} else {
			erg.pot(i / 2);
			erg.mul(this);
		}

		matrix = erg.getMatrixStructure();

	}
}
